<?php

declare (strict_types=1);
namespace App\Model;

use App\Traits\ModelLabelTrait;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property int $supervise_id 
 * @property int $paper_id 
 * @property string $question 
 * @property string $options 
 * @property string $answer_txt 
 * @property string $extend_images 
 * @property string $extend_txt 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property string $deleted_at 
 */
class PositionSuperviseSheet extends Model
{
    use SoftDeletes,ModelLabelTrait;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'position_supervise_sheet';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'supervise_id',
        'paper_id' ,
        'question',
        'options',
        'answer_txt',
        'extend_images',
        'extend_txt',
        'score',
        'lng',
        'lat',
        'address'
    ];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int', 'supervise_id' => 'integer', 'paper_id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    protected $appends=[
        'sheet_status_label',
        //'extend_files'
    ];

    const SHEET_STATUS_DEFAULT = 0;
    const SHEET_STATUS_ASSIGN = 10;
    const SHEET_STATUS_REJECT = 11;
    const SHEET_STATUS_REPLY = 20;
    const SHEET_STATUS_SCORE = 30;

    const SHEET_STATUS_LABELS = [
        self::SHEET_STATUS_DEFAULT => '待答复',
        self::SHEET_STATUS_ASSIGN => '已分派',
        self::SHEET_STATUS_REJECT => '已退回',
        self::SHEET_STATUS_REPLY => '已答复',
        self::SHEET_STATUS_SCORE => '已打分'
    ];


    public function paper(){
        return $this->belongsTo(PositionSupervise::class,'supervise_id','id');
    }

    public function getSheetStatusLabelAttribute(){
        return self::getLabel('sheet_status',$this->sheet_status);
    }

    public function getExtendFilesAttribute(){
        if($this->extend_images&&$this->extend_images!=''){
            $images = explode(',',$this->extend_images);
            foreach ($images as &$image){
                preg_match("/(?:[http|https]:\/\/)([^\/]+)/", $image, $matched);
                if(empty($matched)){
                    $image = env('QINBIU_DOMAIN').$image;
                }
            }

            return $images;
        }
        return [];
    }
}